package lang;

import common.constants.BaseConst;
import entity.TestEntityOne;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

public class StringTest {

    private static final Logger logger = LoggerFactory.getLogger(StringTest.class);

    /**
     * 比较两个字符串的长度
     */
    @Test
    public void stringCompareTo() {
        String str1 = "zhangsan123456";
        String str2 = "zhangsan";
        int num = str1.compareTo(str2);
        logger.info("测试数据结果为：" + num);
    }

    /**
     * charAt(i) 得到第i个字符的值
     */
    @Test
    public void stringCharAt() {
        String str1 = "zhangsan123456";
        char c = str1.charAt(1);
        logger.info(String.valueOf(c));
    }

    /**
     * isEmpty() 字符串不能为null 否则会报控指针
     */
    @Test
    public void stringIsEmpty() {
        String str = null;
        // isEmpty() 报空指针异常
        boolean empty = str.isEmpty();
        logger.info(String.valueOf(empty));
    }


    /**
     * join() 字符串拼接
     */
    @Test
    public void stringJoin() {
        // 拼接字符串
        String str1 = "zhangsan";
        String.join(",", str1);
        logger.info(str1);

        // 拼接迭代器中的元素
        List<String> list = Arrays.asList("zhangsan", "lisi", "wangwu");
        String joinStr = String.join("','", list);
        logger.info(joinStr);
    }

    @Test
    public void testOne() {
        Integer one = 24;
        BigDecimal two = new BigDecimal(23);

        Map<String, Object> map = new HashMap<>();
        map.put("one", one);
        map.put("two", two);
        String strOne = (String) map.get("one");
        String strTwo = (String) map.get("two");

        String strThree = String.valueOf(map.get("one"));
        String strFour = String.valueOf(map.get("two"));
    }

    /**
     * String 强制转换
     */
    @Test
    public void testTwo() {
        TestEntityOne entityOne = new TestEntityOne();
        String str = (String) entityOne.getObject();
        logger.info(str);

        Object object = Integer.valueOf(0);
        entityOne.setObject(object);
        Object object1 = entityOne.getObject();
        // String 强制转换报错 Integer 转 String
        logger.info((String) object1);
    }

    @Test
    public void splitTest() {
        String result = "10 10 10 10";
        String[] split = result.split(" ");
        logger.info(String.valueOf(split));
    }

    /**
     * 匹配 #..# 中间的字符串
     */
    @Test
    public void test1() {
        String sql = " select\n" +
                "    billcode,\n" +
                "    id\n" +
                "from\n" +
                "    table_#one# where 1=1";

        Matcher matcher = BaseConst.PATTERN_ONE.matcher(sql);
        while (matcher.find()) {
            String group = matcher.group();
            logger.info("find str:{}", group);
        }
    }

}
